package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.PerCourse;
import com.zretc.emptraining.entity.RefTable;
import com.zretc.emptraining.service.PerCourseServic;
import com.zretc.emptraining.serviceimpl.PerCourseServiceImp;
import com.zretc.emptraining.serviceimpl.RefTableServiceImp;

/**
 * Servlet implementation class PerCourseServlet
 */
@WebServlet("/PerCourseServlet")
public class PerCourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PerCourseServic pcsi = new PerCourseServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PerCourseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset = utf-8");
		String op = "";

		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if (op.equals("showPercourseByPage") || op.equals("")) {
			// 查询个人所有申请列表信息  -- 根据不同的
			showPerCourse(request, response);
			// 删除个人申请列表信息
		} else if (op.equals("delPerCourse")) {
			delPre(request, response);
			// 在同意过后的可能进行拒绝拒绝
		} else if (op.equals("refusePerCourse")) {
			refPre(request, response);
			// 通过个人课程申请
		} else if (op.equals("passPerCourse")) {
			passPre(request, response);
			// 添加个人课程申请的信息
		} else if (op.equals("addPerCourse")) {
			addPre(request, response);
			// 修改个人课程申请表的信息
		} else if (op.equalsIgnoreCase("updatePerCourse")) {
			updPre(request, response);
			// 批量删除
		} else if (op.equals("delAll")) {
			delAll(request, response);
			//主管页面的个人申请表的信息
		} else if (op.equals("showPerCourseByManager")) {
			showByManager(request, response);
			//员工申请课程
		} else if (op.equals("addPerCourseByEmp")) {
			addPerCourseByEmp(request, response);
		}
	}

	private void addPerCourseByEmp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		
		//这里的courseNo为参培编号注意！！
		int courseNo = Integer.parseInt(request.getParameter("courseNo"));
		System.out.println("员工好"+empNo);
		System.out.println("课程号"+courseNo);
		// 校验在个人课程申请中是否存在申请信息，若已存在则返回false不能申请
		boolean flag1 = pcsi.wExists(empNo, courseNo);
		// 校验在参培表中是否存在员工参加同一个课程的参培信息，若参培信息已存在则返回false说明不用申请
		RefTableServiceImp rtsi = new RefTableServiceImp();
		boolean flag2 = rtsi.diae(courseNo, empNo);
		//
		boolean flag3 = pcsi.wCourseCanAdd(courseNo);
		// 如果两个都为true则说明可以添加
		if (flag1 == true && flag2 == true && flag3 == true) {
			
			PerCourse perCourse = new PerCourse(empNo, courseNo, null);
			boolean flag = pcsi.addPerCourse(perCourse);
			
			Gson gson = new Gson();
			String jsonString = gson.toJson(flag);
			PrintWriter printWriter = response.getWriter();
			printWriter.print(jsonString);
			printWriter.close();
			// 一个不为true则不能添加。
		} else {
			
			boolean flag4 = (flag1==true && flag2 == true && flag3 == true);
			Gson gson = new Gson();
			String jsonString = gson.toJson(flag4);
			PrintWriter printWriter = response.getWriter();
			printWriter.print(jsonString);
			printWriter.close();
		}
	}

	private void showByManager(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// String fy = request.getParameter("fy");
		int deptNo = Integer.parseInt(request.getParameter("deptNo"));
		String empno = request.getParameter("empNo");
		if (empno == null || empno.equals("")) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourseBydeptNo(page, pageSize, deptNo);

			request.setAttribute("pd", pd);

			request.getRequestDispatcher("emp-apl.jsp").forward(request, response);
		} else if (empno != null) {
			int empNo = Integer.parseInt(empno);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourseBydeptNoWithEmpNo(page, pageSize, deptNo, empNo);
			request.setAttribute("empNo", empNo);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("emp-apl.jsp").forward(request, response);
		}
	}

	private void delAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String deleteAll = request.getParameter("deleteAll");
		System.out.println("deleteall:" + deleteAll);
		deleteAll = deleteAll.substring(0, deleteAll.length() - 1);
		String[] strs = deleteAll.split(",");
		boolean flag = false;
		for (int i = 0; i < strs.length; i++) {
			flag = pcsi.delPerCourseByAdmin(Integer.parseInt(strs[i]));
		}
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void updPre(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		int perCourseNo = Integer.parseInt(request.getParameter("perCourseNo"));
		PerCourse perCourse = new PerCourse(perCourseNo, empNo, cpNo);
		boolean flag = pcsi.updatePerCourse(perCourse);
		System.out.println("update:" + flag);
		System.out.println("empNo:" + empNo);
		System.out.println("courseNo:" + cpNo);
		System.out.println("perCourseNo:" + perCourseNo);
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void addPre(HttpServletRequest request, HttpServletResponse response) throws IOException {
		addPerCourseByEmp(request, response);
	}

	private void passPre(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int perCourseNo = Integer.parseInt(request.getParameter("perCourseNo"));
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		boolean flag = pcsi.passPerCourse(perCourseNo);
		RefTableServiceImp rtsi = new RefTableServiceImp();

		RefTable refTable = new RefTable(empNo, cpNo);
		boolean flag1 = rtsi.addRefTable(refTable);
		System.out.println(flag1);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);

		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	private void refPre(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int perCourseNo = Integer.parseInt(request.getParameter("perCourseNo"));
		boolean flag = pcsi.refusePerCourse(perCourseNo);
//				System.out.println(flag);
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		RefTableServiceImp rsti = new RefTableServiceImp();
		boolean flag1 = rsti.delRefWhereDelMA(empNo, cpNo);
		System.out.println(flag1);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	private void delPre(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int perCourseNo = Integer.parseInt(request.getParameter("perCourseNo"));
		boolean flag = pcsi.delPerCourseByAdmin(perCourseNo);
//				System.out.println(flag);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
//				System.out.println(jsonString);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	private void showPerCourse(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String select = "";
		if (request.getParameter("select") != null) {
			select = request.getParameter("select");
		}
		String keyword = "";
		if(request.getParameter("key") != null) {
			keyword = request.getParameter("key");
		}
		// 查询个人申请表的所有信息
		if (select.equals("") ||keyword.equals("")) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourse(page, pageSize);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("PerCourseList.jsp").forward(request, response);

			// 根据员工编号查询
		} else if (select.equals("员工号")) {
			int key = Integer.parseInt(keyword);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourseByempNo(page, pageSize, key);
			request.setAttribute("key", key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("PerCourseList.jsp").forward(request, response);
			// 根据部门编号查询
		} else if (select.equals("deptNo")) {
			int key = Integer.parseInt(keyword);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourseBydeptNo(page, pageSize, key);
			request.setAttribute("key", key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("PerCourseList.jsp").forward(request, response);
			// 根据课程计划编号查询
		} else if (select.equals("课程计划号")) {
			int key = Integer.parseInt(keyword);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<PerCourse> pd = new PageData<PerCourse>();
			pd = pcsi.getPerCourseByCpNo(page, pageSize, key);
			request.setAttribute("key", key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("PerCourseList.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
